// app/model/sales_order_model.js
'use strict';

module.exports = app => {
  const { STRING, INTEGER, DECIMAL, DATE, ENUM, BOOLEAN } = app.Sequelize;

  const SalesOrder = app.model.define('salesOrder', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    organizationId: {
      type: INTEGER,
      allowNull: false,
      field: 'organization_id',
      comment: '组织ID'
    },
    orderNo: {
      type: STRING(50),
      allowNull: false,
      unique: true,
      field: 'order_no',
      comment: '订单编号'
    },
    platformOrderId: {
      type: STRING(100),
      allowNull: false,
      unique: true,
      field: 'platform_order_id',
      comment: '平台订单号'
    },
    customerId: {
      type: INTEGER,
      allowNull: false,
      field: 'customer_id',
      comment: '客户ID'
    },
    orderAmount: {
      type: DECIMAL(10, 2),
      allowNull: false,
      field: 'order_amount',
      comment: '订单金额'
    },
    orderDate: {
      type: DATE,
      allowNull: false,
      field: 'order_date',
      comment: '订单日期'
    },
    invoiceStatus: {
      type: ENUM('uninvoiced', 'invoiced', 'sent'),
      defaultValue: 'uninvoiced',
      field: 'invoice_status',
      comment: '开票状态'
    },
    invoiceDate: {
      type: DATE,
      field: 'invoice_date',
      comment: '开票日期'
    },
    invoiceNumber: {
      type: STRING(50),
      field: 'invoice_number',
      comment: '发票号码'
    },
    orderStatus: {
      type: ENUM('pending', 'completed', 'cancelled'),
      defaultValue: 'completed',
      field: 'order_status',
      comment: '订单状态'
    },
    // 添加平台来源字段
    platformSource: {
      type: STRING(50),
      field: 'platform_source',
      comment: '平台来源'
    },
    // 添加是否含赠品字段
    isGift: {
      type: BOOLEAN,
      defaultValue: false,
      field: 'is_gift',
      comment: '是否含赠品'
    },
    // 负责销售字段
    responsibleSalesman: {
      type: STRING(100),
      field: 'responsible_salesman',
      comment: '负责销售'
    },
    // 催款相关字段
    collectionStatus: {
      type: ENUM('pending', 'contacted', 'promised', 'paid', 'difficult'),
      defaultValue: 'pending',
      field: 'collection_status',
      comment: '催款状态'
    },
    collectionCount: {
      type: INTEGER,
      defaultValue: 0,
      field: 'collection_count',
      comment: '催款次数'
    },
    lastCollectionDate: {
      type: DATE,
      field: 'last_collection_date',
      comment: '最后催款日期'
    },
    lastCollectionResult: {
      type: STRING(200),
      field: 'last_collection_result',
      comment: '最后催款结果'
    },
    nextFollowupDate: {
      type: DATE,
      field: 'next_followup_date',
      comment: '下次跟进日期'
    },
    // 新增的采购和供应商信息字段
    purchaserName: {
      type: STRING(100),
      field: 'purchaser_name',
      comment: '采购人员姓名'
    },
    purchaserOrganizationId: {
      type: INTEGER,
      field: 'purchaser_organization_id',
      comment: '采购单位ID'
    },
    purchaserOrganizationName: {
      type: STRING(200),
      field: 'purchaser_organization_name',
      comment: '采购单位名称'
    },
    supplierName: {
      type: STRING(100),
      field: 'supplier_name',
      comment: '供应商联系人姓名'
    },
    supplierOrganizationId: {
      type: INTEGER,
      field: 'supplier_organization_id',
      comment: '供应商组织ID'
    },
    supplierOrganizationName: {
      type: STRING(200),
      field: 'supplier_organization_name',
      comment: '供应商组织名称'
    }
  }, {
    tableName: 'sales_orders',
    timestamps: false
  });

  // 添加关联关系
  SalesOrder.associate = function() {
    // 销售订单与客户的关联
    if (app.model.CustomerModel) {
      SalesOrder.belongsTo(app.model.CustomerModel, {
        foreignKey: 'customerId',
        as: 'customer'
      });
    }

    // 销售订单与组织的关联
    if (app.model.OrganizationModel) {
      SalesOrder.belongsTo(app.model.OrganizationModel, {
        foreignKey: 'organizationId',
        as: 'organization'
      });
    }
    
    // 销售订单与订单详情的关联
    if (app.model.SalesOrderItemModel) {
      SalesOrder.hasMany(app.model.SalesOrderItemModel, {
        foreignKey: 'orderId',
        as: 'items'
      });
    }
  };

  return SalesOrder;
};